export const states = {
  waiting: '待受理',
  accepted: '已受理',
  processing: '处理中',
  resolved: '已解决',
  confirming: '待确认',
  closed: '已关闭',
};

export const types = {
  bug: 'bug提交',
  using: '使用问题',
  need: '功能需求',
  suggest: '改进建议',
  other: '其它',
};

const formatReplyHash = replyHash => Object.values(replyHash).map((hash) => {
  try {
    return JSON.parse(hash);
  } catch (err) {
    return { content: '解析JSON格式出错' };
  }
});

const formatContents = feedback => [{ content: feedback.content, timestamp: feedback.created_at }]
  .concat(formatReplyHash(feedback.reply_hash));

export const formatFeedback = feedback => ({
  id: feedback.id,
  title: feedback.title,
  state: feedback.state,
  stateCN: states[feedback.state],
  type: feedback.tag,
  typeCN: types[feedback.tag],
  contents: formatContents(feedback),

  userId: feedback.user.id,
  userNickname: feedback.user.nickname,
  userAvatar: feedback.user.avatar,

  created_at: feedback.created_at,
});

export const formatFeedbackList = list => list.map(item => formatFeedback(item));
